package tree;

import java.util.*;

public class BinaryTreeTest {
    static Object[] a = new Object[]{"G","D","A"," "," ","F","E"," "," "," ","M","H","Z"};

    static List<Object> abc = Arrays.asList(a);

    public static void main(String[] args) {
        BinaryTreeNode root = new BinaryTreeNode(a[0]);
        List list = Arrays.asList(a);
        BinaryTree tree = new BinaryTree(list);
        tree.root = tree.frontadd();
        tree.frontData(tree.root);
        System.out.println("------------------------");
        tree.middleData(tree.root);
    }

}

class BinaryTreeNode {

    Object data;

    BinaryTreeNode left;

    BinaryTreeNode right;

    public BinaryTreeNode() {
    }

    public BinaryTreeNode(Object data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "BinaryTreeNode{" +
                "data=" + data +
                ", left=" + left +
                ", right=" + right +
                '}';
    }
}

class BinaryTree {

    BinaryTreeNode root;

    ArrayDeque deque = new ArrayDeque();


    public BinaryTree() {
    }

    public BinaryTree(List list) {
        this.setData(list);
    }

    Object readStack() {
        if (!deque.isEmpty()) {
            String stack = deque.poll().toString();
            //System.out.println("读取数据:"+stack);
            return stack;
        }
        return null;
    }

    BinaryTreeNode frontadd() {
        Object data = readStack();
        if ((data==null)||(data.equals(" "))){
            return null;
        }
        BinaryTreeNode node = new BinaryTreeNode();
        node.data = data;
        node.left =  frontadd();
        node.right = frontadd();
        return  node;
    }

    void frontData(BinaryTreeNode node) {
        if (node == null) {
            return;
        }
        System.out.println(node.data);
        frontData(node.left);
        frontData(node.right);
    }

    void middleData(BinaryTreeNode node) {
        if (node == null) {
            return;
        }
        frontData(node.left);
        System.out.println(node.data);
        frontData(node.right);
    }

    void setData(List list) {
        for (int i = 0; i < list.size(); i++) {
            deque.offer(list.get(i));
        }
        //System.out.println("队列数量"+deque.size());
    }
}
